fix(tools): Handle JSON Schema boolean schemas in Gemini schema conversion#4531
fix(tools): Handle JSON Schema boolean schemas in Gemini schema conversion#4531onematchfox wants to merge 2 commits intogoogle:mainfrom
Conversation
…rsion
JSON Schema allows `true` and `false` as valid boolean schemas, where
`true` accepts any value and `false` rejects all values. Some MCP servers
use this pattern for unconstrained fields. The schema sanitizer previously
passed booleans through unchanged, causing a Pydantic ValidationError when
`_ExtendedJSONSchema` tried to validate them as schema objects.
Convert boolean schemas to `{"type": "object"}` as the closest
approximation available in Gemini's schema model.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Brian Fox <878612+onematchfox@users.noreply.github.com>
Summary of ChangesHello @onematchfox, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a Pydantic ValidationError that occurred when the Gemini schema conversion process encountered boolean JSON schemas (e.g., Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
The pull request correctly addresses the Pydantic ValidationError encountered when processing JSON schemas that use boolean values (e.g., "field": true), which is a valid pattern in JSON Schema (Draft 7+) often used by MCP servers to indicate unconstrained fields. By converting these boolean schemas to an unconstrained object schema ({"type": "object"}), the utility now handles these cases gracefully instead of crashing. The implementation is correctly placed within the recursive sanitization logic and is supported by comprehensive unit tests covering various nesting scenarios including properties and array items.
|
Hi @onematchfox , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share. |
|
Hi @xuanyang15 , can you please review this. LGTM. |
|
Started internal review process |
…rsion Merge #4531 **Problem:** JSON Schema allows `true` and `false` as valid boolean schemas, where `true` accepts any value and `false` rejects all values. Some MCP servers use this pattern for unconstrained fields. E.g. [mcp-grafana](https://github.com/grafana/mcp-grafana) - see [grafana-mcp-list-tools.json](https://github.com/user-attachments/files/25392430/grafana-mcp-list-tools.json) which was obtained from `tools/list` The schema sanitizer previously passed booleans through unchanged, causing a Pydantic ValidationError when `_ExtendedJSONSchema` tried to validate them as schema objects. ``` 1 validation error for _ExtendedJSONSchema properties.data.items.properties.model Input should be a valid dictionary or object to extract fields from [type=model_attributes_type, input_value=True, input_type=bool] For further information visit https://errors.pydantic.dev/2.12/v/model_attributes_type Traceback (most recent call last): ... File "/.foo/.venv/lib/python3.13/site-packages/google/adk/runners.py", line 561, in run_async async for event in agen: yield event File "/.foo/.venv/lib/python3.13/site-packages/google/adk/runners.py", line 549, in _run_with_trace async for event in agen: yield event File "/.foo/.venv/lib/python3.13/site-packages/google/adk/runners.py", line 778, in _exec_with_plugin async for event in agen: ...<64 lines>... yield event File "/.foo/.venv/lib/python3.13/site-packages/google/adk/runners.py", line 538, in execute async for event in agen: yield event File "/.foo/.venv/lib/python3.13/site-packages/google/adk/agents/base_agent.py", line 294, in run_async async for event in agen: yield event File "/.foo/.venv/lib/python3.13/site-packages/google/adk/agents/llm_agent.py", line 468, in _run_async_impl async for event in agen: ...<5 lines>... should_pause = True File "/.foo/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 427, in run_async async for event in agen: last_event = event yield event File "/.foo/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 446, in _run_one_step_async async for event in agen: yield event File "/.foo/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 578, in _preprocess_async await tool.process_llm_request( tool_context=tool_context, llm_request=llm_request ) File "/.foo/.venv/lib/python3.13/site-packages/google/adk/tools/base_tool.py", line 129, in process_llm_request llm_request.append_tools([self]) ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^ File "/.foo/.venv/lib/python3.13/site-packages/google/adk/models/llm_request.py", line 255, in append_tools declaration = tool._get_declaration() File "/.foo/.venv/lib/python3.13/site-packages/google/adk/tools/mcp_tool/mcp_tool.py", line 200, in _get_declaration parameters = _to_gemini_schema(input_schema) File "/.foo/.venv/lib/python3.13/site-packages/google/adk/tools/_gemini_schema_util.py", line 218, in _to_gemini_schema json_schema=_ExtendedJSONSchema.model_validate(sanitized_schema), ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^ File "/.foo/.venv/lib/python3.13/site-packages/pydantic/main.py", line 716, in model_validate return cls.__pydantic_validator__.validate_python( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ obj, ^^^^ ...<5 lines>... by_name=by_name, ^^^^^^^^^^^^^^^^ ) ^ pydantic_core._pydantic_core.ValidationError: 1 validation error for _ExtendedJSONSchema properties.data.items.properties.model Input should be a valid dictionary or object to extract fields from [type=model_attributes_type, input_value=True, input_type=bool] For further information visit https://errors.pydantic.dev/2.12/v/model_attributes_type ``` **Solution:** Convert boolean schemas to `{"type": "object"}` as the closest approximation available in Gemini's schema model. Co-authored-by: Xuan Yang <xygoogle@google.com> COPYBARA_INTEGRATE_REVIEW=#4531 from onematchfox:fix-gemini-schema-bool 383ac0c PiperOrigin-RevId: 875219362
|
Thank you @onematchfox for your contribution! 🎉 Your changes have been successfully imported and merged via Copybara in commit 3256a67. Closing this PR as the changes are now in the main branch. |
Problem:
JSON Schema allows
trueandfalseas valid boolean schemas, wheretrueaccepts any value andfalserejects all values. Some MCP servers use this pattern for unconstrained fields. E.g. mcp-grafana - see grafana-mcp-list-tools.json which was obtained fromtools/listThe schema sanitizer previously passed booleans through unchanged, causing a Pydantic ValidationError when
_ExtendedJSONSchematried to validate them as schema objects.Solution:
Convert boolean schemas to
{"type": "object"}as the closest approximation available in Gemini's schema model.Testing Plan
Unit Tests:
Checklist